package com.dhms.vostok1.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import com.dhms.basebusiness.databinding.BaseListItemBinding
import com.dhms.vostok1.data.BaseListItem
import com.dhms.vostok1.data.OnBaseListItemClickListener

class BaseListAdapter(
    context: Context,
    private var items: List<BaseListItem> = arrayListOf(),
    private val onItemClickListener: OnBaseListItemClickListener? = null
) :
    ArrayAdapter<BaseListItem>(context, 0, items) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val binding = convertView?.tag as? BaseListItemBinding ?: BaseListItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        binding.root.tag = binding

        binding.item = items[position]

        binding.baseListItemWrapper.setOnClickListener {
            onItemClickListener?.let { it1 -> it1(position) }
        }

        return binding.root
    }

    override fun getCount(): Int {
        return items.size
    }
}